package wh;

import wh.exceptions.BadIDFormat;
import wh.exceptions.IDCollisionException;
import wh.exceptions.InvalidValueException;
import wh.iolists.StorageDiskList;

public class Customer {

	private String customerID;
	private String name;
	private String customerType;
	private static final String CUSTOMER_IN_ID = "cus"; // Constant class value, avoiding duplication of this value for each object
	private static final String CUSTOMER_REGULAR = "regular";
	private static final String CUSTOMER_SUPER = "super";
	
	/**
	 * 
	 * @param customerID
	 * @param name
	 * @param customerType
	 * @throws BadIDFormat
	 * @throws IDCollisionException
	 * @throws InvalidValueException
	 */
	public Customer(String customerID, String name, String customerType) throws BadIDFormat, IDCollisionException, InvalidValueException {
		
		this.customerID = customerID.trim();
		this.name = name.trim();
		this.customerType = customerType.trim();
		
		validateID();
		validateInput();
	}
	
	
	/**
	 * 
	 * The customer ID begins with "cus".
	 * Integer value in the middle does not have a fixed limit (other than what 32bit int can hold).
	 * The last three characters is taken from the two first chars in the name, and the first char from the last name
	 * 
	 * @throws BadIDFormat
	 * @throws IDCollisionException
	 */
	private void validateID() throws BadIDFormat, IDCollisionException
	{
		System.out.println("About to validate Customer");
		if (customerID != null) {
			
			char[] firstThreeData = { customerID.charAt(0), customerID.charAt(1), customerID.charAt(2) };
			String ordString = new String(firstThreeData);
			
			if (ordString.equals(CUSTOMER_IN_ID)) // This should always be "cus".
			{
				// Progressing in the validation ..
				int stringSize = customerID.length();
				// From the size of the ID string (the end index position in it) I count back three positions to get to the
				// first character of the final three, and construct the string from there.
				char finalStartChar = customerID.charAt(stringSize-3);
				char[] finalSixData = { finalStartChar, customerID.charAt(stringSize-2), customerID.charAt(stringSize-1) };
				
				String endingString = new String(finalSixData);
				
				// Making sure that the digits of the unique integer has not spilled over to the last three positions of the ID 
				if (!Character.isDigit(finalStartChar)) {
					for (char c : finalSixData) // for-each is appropriate here
					{
						if (Character.isDigit(c))
							throw new BadIDFormat(customerID, endingString);
					}
				}
				// So far so good, checking if the ID is unique for Order
				StorageDiskList.CompareProductIDs(customerID); // This can throw exception
			}
			else
				throw new BadIDFormat(customerID, ordString);
			// At this point the order ID is valid.
			System.out.println("Customer object valid");
		}
	}
	
	/**
	 * Making sure that we have a full name.
	 * And that the customer falls in one of the two available categories.
	 * @throws InvalidValueException
	 */
	private void validateInput() throws InvalidValueException
	{
		if (!(name == null || customerType == null)) {
			
			if (name.length() < 3)
				throw new InvalidValueException("Customer name", name, "Too short!");
			
			if (!(customerType.equals(CUSTOMER_REGULAR) || customerType.equals(CUSTOMER_SUPER)))
				throw new InvalidValueException("Customer type", customerType, "Undefined category!");
		}
		else 
			throw new IllegalArgumentException("Null exception! Name: "+name+". Customer type: "+customerType);
	}
	
	public String getCustomerID()
	{
		return customerID;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String getCustomerType()
	{
		return customerType;
	}
}
